import 'dart:developer';

import 'package:FlutterDemo/common/commons.dart';
import 'package:flutter/material.dart';

/// 构建 测试用的pageview
buildSamplePageView() {
  return Container(
    height: 220,
    margin: EdgeInsets.only(top: 15),
    child: PageView(
      controller: PageController(viewportFraction: 0.9),
      children: buildSamplePageViewChildRen(),
    ),
  );
}

/// 构建PageView中的child
buildSamplePageViewChildRen() {
  List<Widget> widgets = <Widget>[];
  for (var i = 0; i < 10; i++) {
    List<MaterialColor> primaries = Colors.primaries;
    widgets.add(buildPageItem(primaries[i], i, Color(primaries[i + 1].value + 30)));
  }
  return widgets;
}

/// 构建PageItem
buildPageItem(Color color, int index, Color txtColor) {
  return Container(
    color: color,
    margin: EdgeInsets.only(left: index > 0 ? 10 : 0),
    alignment: Alignment.center,
    child: Text("page $index",
        style: TextStyle(color: txtColor, fontSize: 20, fontStyle: FontStyle.italic, fontWeight: FontWeight.bold)),
  );
}

/// 构建图片列表
buildImgListPageView() {
  return Container(
    height: 200,
    padding: EdgeInsets.only(left: 20, right: 20, top: 20, bottom: 20),
    child: PageView(
      children: buildSampleImgList(),
    ),
  );
}

/// 垂直滚动的pageview
buildVerticalListPageView() {
  return Container(
    height: 200,
    padding: EdgeInsets.only(left: 20, right: 20),
    child: PageView(
      // 界面发生变化时回调
      onPageChanged: (pos) {
        log("当前page: $pos");
      },
      // 控制垂直滚动
      scrollDirection: Axis.vertical,
      // 控制每一个Page 不占满
      controller: PageController(viewportFraction: 0.9),
      children: buildSamplePageViewChildRenVertical(),
    ),
  );
}

/// 构建PageView中的child
buildSamplePageViewChildRenVertical() {
  List<Widget> widgets = <Widget>[];
  for (var i = 0; i < 10; i++) {
    List<MaterialColor> primaries = Colors.primaries;
    widgets.add(buildVerticalPageItem(primaries[i], i, Color(primaries[i + 1].value + 30)));
  }
  return widgets;
}

/// 构建PageItem
buildVerticalPageItem(Color color, int index, Color txtColor) {
  return Container(
    color: color,
    margin: EdgeInsets.only(top: index > 0 ? 10 : 0),
    alignment: Alignment.center,
    child: Text("page $index",
        style: TextStyle(color: txtColor, fontSize: 20, fontStyle: FontStyle.italic, fontWeight: FontWeight.bold)),
  );
}

/// 带指示器效果：无限循环轮播
buildSampleBanner() {
  return SampleBannerWidget();
}

/// 无限循环轮播
class SampleBannerWidget extends StatefulWidget {
  const SampleBannerWidget({Key? key}) : super(key: key);

  @override
  _SampleBannerWidgetState createState() => _SampleBannerWidgetState();
}

class _SampleBannerWidgetState extends State<SampleBannerWidget> {
  var titles = ["PageView1", "PageView2", "PageView3"];

  // 当前索引
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 20, bottom: 20, left: 10, right: 10),
      child: Stack(
        children: [
          // pageView
          Container(
            height: 200,
            child: PageView.builder(
              onPageChanged: (index) {
                // 更新指示器
                setState(() {
                  _currentIndex = index % titles.length;
                });
              },
              itemBuilder: (context, index) {
                var pos = index % titles.length;
                return buildBannerItem(titles[index % titles.length], pos);
              },
              itemCount: 10000,
            ),
          ),
          // 指示器
          Positioned(
              bottom: 10, // 距离底部10
              left: 0,
              right: 0,
              child: Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(titles.length, (index) {
                    return Container(
                      margin: EdgeInsets.symmetric(horizontal: 5),
                      width: 10,
                      height: 10,
                      decoration: BoxDecoration(
                          shape: BoxShape.circle, color: _currentIndex == index ? Colors.blue : Colors.grey),
                    );
                  }),
                ),
              ))
        ],
      ),
    );
  }

  buildBannerItem(var title, var index) {
    return Container(
      color: Colors.primaries[index + 1],
      alignment: Alignment.center,
      child: Text(
        title,
        style: TextStyle(color: Colors.white, fontSize: 28),
      ),
    );
  }
}
